package main

import (
	"sync"
)

var initOnce sync.Once

type ExampleBroker struct {
	Suber map[string][]BrokerCallback
}

func NewBroker() *ExampleBroker {
	e := &ExampleBroker{}
	e.Suber = map[string][]BrokerCallback{}
	return e
}

func (e *ExampleBroker) Pub(msg Message) {
	if _, ok := e.Suber[msg.Topic]; ok {
		for _, callback := range e.Suber[msg.Topic] {
			go callback(msg)
		}
	}
}

func (e *ExampleBroker) Sub(topic string, callback BrokerCallback) {
	if _, ok := e.Suber[topic]; !ok {
		e.Suber[topic] = []BrokerCallback{}
	}
	e.Suber[topic] = append(e.Suber[topic], callback)
}
